package com.framework.utils;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * DOM4J解析XML文件
 */
public class Dom4JXML {


    public static void DOM$J(String database, String tableNames, String domainObjectNames) {
        SAXReader reader = new SAXReader();
        InputStream is = Dom4JXML.class.getClassLoader().getResourceAsStream("generatorConfig.xml");
        Document doc = null;

        try {
            doc = reader.read(is);

            Element root = doc.getRootElement();
            List elements = root.elements("context");
            for (Iterator<?> it = elements.iterator(); it.hasNext(); ) {
                Element e = (Element) it.next();
                Element table = e.element("table");
                Element jdbcConnection = e.element("jdbcConnection");
                Attribute tableName = table.attribute("tableName");
                Attribute connectionURL = jdbcConnection.attribute("connectionURL");
                Attribute domainObjectName = table.attribute("domainObjectName");
                tableName.setText(tableNames);
                connectionURL.setText(""+database+"?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true&amp;serverTimezone=Asia/Shanghai\" userId=\"Hsapi\" password=\"Hsapi-2016");
                domainObjectName.setText(domainObjectNames);
                System.out.println(table);
            }
            XMLUtil.writeXml(doc, "E:\\MyProject\\universal-framework\\framework-generator\\src\\main\\resources\\generatorConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}